<?php
namespace app\api\controller;

use app\common\controller\ApiController;
use Symfony\Component\Yaml\Dumper;
use think\App;
use think\facade\Db;

class EnjoySellingCustomers extends ApiController
{
    protected $noNeedLogin = ['createAccountObj',"create_Customer_complaint","create_equipment","getOpenUserId"];
    protected $mustLogin = [];
 
     /**
     * 获取token
     */
    public function corpAccessToken()
    {
        $method = 'POST';
        $appId = 'FSAID_131b260';
        $appSecret = 'bc3f3bd184c54895aca794db584f36fa';
        $permanentCode = 'C4E757336822DECDBBBB2C6117ABF449';
        $url = 'https://open.fxiaoke.com/cgi/corpAccessToken/get/V2';
        $header = [
                         'Content-Type:application/json'
                   ];
        $res =  json_encode(compact('appId', 'appSecret', 'permanentCode'));
        $data = $this->http_curl($url,$method, $res, $header, $timeout = 10); 
        
        if(!empty($data) && isset($data) && $data['errorCode'] ==0 && $data['errorMessage'] == 'success' )
        {
            $data['addtime'] = time();
            if(Db::name('atoken')->where('id',1)->find())
            {
               Db::name('atoken')->where('id',1)->update($data);  
            }else{
               Db::name('atoken')->insert($data);  
            }
           return $data;
        }
        
    }

    /**
    * 获取OpenUserId
    */
    public function getOpenUserId()
    {
        $url = 'https://open.fxiaoke.com/cgi/user/getByMobile';
        $qwe = $this->corpAccessToken();
        $corpAccessToken = $qwe['corpAccessToken'];   
        $corpId = $qwe['corpId'];
        $mobile = "18192572972";
        $data = [
            "corpAccessToken"=>$corpAccessToken,
            "corpId"=>$corpId,
            "mobile"=>$mobile,
        ];
        $res = json_encode($data, JSON_UNESCAPED_UNICODE);
        $method = 'POST';
        $header = [
                    'Content-Type:application/json'
                  ];
        $dsdf = $this->http_curl($url,$method, $res, $header, $timeout = 10);
        dump($dsdf);die;
    }

    /**
    * 添加客户
    */
    public function createAccountObj()
    {
       $param = $this->request->param();
    //    if(empty($param['owner'])){
    //         file_put_contents('create_SalesOrder.log', date("Y-m-d H:i:s",time()).": 请求结束！负责人不能为空".PHP_EOL, FILE_APPEND);
    //         $this->error("请求结束！负责人不能为空");
    //     }
        $url = 'https://open.fxiaoke.com/cgi/crm/v2/data/create';  // 创建客户对象
 
        $qwe = $this->corpAccessToken();
        $corpAccessToken = $qwe['corpAccessToken'];   
        $corpId = $qwe['corpId'];
        $currentOpenUserId = 'FSUID_B9743F457C3ABD3C3FA9473741D905DE';
        
        $rst['dataObjectApiName'] = 'AccountObj';
        $rst["name"] = "测试007";
        
        $qwe = [
            "object_data"=>$rst,
            'details"'=>[]
        ];
        $arr = [
            "corpAccessToken"=>$corpAccessToken,
            "corpId"=>$corpId,
            "currentOpenUserId"=>$currentOpenUserId,
            "data"=>$qwe,
        ];
 
        $res = json_encode($arr, JSON_UNESCAPED_UNICODE);
        $method = 'POST';
        $header = [
                    'Content-Type:application/json'
                  ];
        $dsdf = $this->http_curl($url,$method, $res, $header, $timeout = 10);
        dump($dsdf);die;

       if($dsdf['errorMessage'] == 'OK'&& $dsdf['errorCode'] == 0)
       {
            file_put_contents('EnjoySellingCustomers.log', date('Y-m-d H:i:s').': 请求成功'.PHP_EOL, FILE_APPEND);
            $this->success("请求成功!",$dsdf);
        }else{
            file_put_contents('EnjoySellingCustomers.log', date('Y-m-d H:i:s').': 请求失败'.PHP_EOL, FILE_APPEND);
            $this->error("请求失败!");
        }
    }


    /**
    * 添加客诉
    */
    public function create_Customer_complaint()
    {
       // owner 18394181267   220699002 合同评审编码 客户 特变电工山东鲁能泰山电缆有限公司
       $param = $this->request->param();
       if(empty($param['owner'])){
            file_put_contents('create_Customer_complaint.log', date("Y-m-d H:i:s",time()).": 请求结束！负责人不能为空".PHP_EOL, FILE_APPEND);
            $this->error("请求结束！负责人不能为空");
        }
        //$owner_id = $this->getByMobile($param['owner']);//负责人  根据手机号查询员工
        $flag = $this->getByNickName($param['owner']);//通过员工昵称查询员工
      dump($flag);die;
        if($flag["code"] == 1){
            $owner_id = [$flag["data"]];
        }else{
            $this->error($flag["msg"]);
        }

        // $gl_customer = $this->gl_customer($param['field_voK4q__c']);//客户
        // if(empty($gl_customer)){
        //     file_put_contents('create_SalesOrder.log', date("Y-m-d H:i:s",time()).": 请求结束！客户关联错误".PHP_EOL, FILE_APPEND);
        //     $this->error("客户关联错误");
        // }
       
        $url = 'https://open.fxiaoke.com/cgi/crm/custom/v2/data/create';  // 创建客诉对象 
        $create_time = substr(microtime(time())*1000,0,13);
        $data = [];
        $data['object_data']["owner"] = $owner_id;
        $data['object_data']['field_ltuG2__c'] = "wm2yVHBwAA-qMoabAUDxvgT4gMuD5u4g";
        $data['object_data']['create_time'] = $create_time;
        $data['object_data']['dataObjectApiName'] = 'object_wtJ5y__c';
        
        $res = $this->qqtbff($data);
        $dsdf = $this->htcurl($url, json_encode($res));
        dump($dsdf);die;

       if($dsdf['errorMessage'] == 'OK'&& $dsdf['errorCode'] == 0)
       { 
            file_put_contents('create_Customer_complaint.log', date('Y-m-d H:i:s').': 请求成功'.PHP_EOL, FILE_APPEND);
            $this->success("请求成功!",$dsdf);
        }else{
            file_put_contents('create_Customer_complaint.log', date('Y-m-d H:i:s').': 请求失败'.PHP_EOL, FILE_APPEND);
            $this->success("请求失败!");
        }
    }


    /**
    * 添加设备
    */
    public function create_equipment()
    {
       $param = $this->request->param();
       if(empty($param['owner'])){
            file_put_contents('create_Customer_complaint.log', date("Y-m-d H:i:s",time()).": 请求结束！负责人不能为空".PHP_EOL, FILE_APPEND);
            $this->error("请求结束！负责人不能为空");
        }
        //$owner_id = $this->getByMobile($param['owner']);//负责人  根据手机号查询员工
        $flag = $this->getByNickName($param['owner']);//通过员工昵称查询员工
        if($flag["code"] == 1){
            $owner_id = [$flag["data"]];
        }else{
            $this->error($flag["msg"]);
        }
       
        $url = 'https://open.fxiaoke.com/cgi/crm/custom/v2/data/create';  // 创建客户对象 
        $create_time = substr(microtime(time())*1000,0,13);
        $data = [];
        $data['object_data']["owner"] = $owner_id;
        $data['object_data']['name'] = "";
        $data['object_data']['create_time'] = $create_time;
        $data['object_data']['dataObjectApiName'] = 'object_06416__c';
        
        $res = $this->qqtbff($data);
        $dsdf = $this->htcurl($url, json_encode($res));
        dump($dsdf);die;

       if($dsdf['errorMessage'] == 'OK'&& $dsdf['errorCode'] == 0)
       { 
            file_put_contents('create_Customer_complaint.log', date('Y-m-d H:i:s').': 请求成功'.PHP_EOL, FILE_APPEND);
            $this->success("请求成功!",$dsdf);
        }else{
            file_put_contents('create_Customer_complaint.log', date('Y-m-d H:i:s').': 请求失败'.PHP_EOL, FILE_APPEND);
            $this->success("请求失败!");
        }
    }
}
